package com.cgw.game.ui;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.cgw.game.R;
import com.cgw.game.view.GameView;

public class MainActivity extends AppCompatActivity {
    private GameView game;
    private Button start;
    private float count = 60f;
    int trueConnt = 0;
    int falseConnt = 0;
    int countTime = 0;
    private TextView show;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle(getIntent().getStringExtra("title"));
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();//返回
            }
        });
        game = findViewById(R.id.game);
        start = findViewById(R.id.start);
        show = findViewById(R.id.show);
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                game.startGame(301000);
                start.setEnabled(false);
            }
        });
        game.setLinsener(new GameView.OnGameLinsener(){
            @Override
            public void onEnd() {
                start.setEnabled(true);
                show.setText("正确率："+trueConnt/count+"%-漏过率:"+(count-trueConnt-falseConnt)/count+"%-错误率："+falseConnt/count+"%\n平均时间："+countTime/trueConnt+"ms");
            }

            @Override
            public void onTonch(boolean isTonch, int differenceTime) {
               if(isTonch){
                   trueConnt++;
                   countTime = countTime+differenceTime;
               }
               if(!isTonch&&differenceTime>0){
                   falseConnt++;
               }
            }
        });
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(),DashboardActivity.class));
            }
        });
    }
}
